""" DigitalICS: mobile data collection tool to complete surveys with integrated multimedia

    Copyright (C) 2009. Yael Schwartzman  

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
    
    Contact information: Yael Schwartzman -  yaelsf@gmail.com
    """
import os
try: import e32
except: import view.pseudoe32 as e32

from api.lang import Lang,Properties
p = Properties()
l = Lang(p.LANGUAGE)

class FExplorer:
    """ Deals with saving and loading files. Initializes variables according
    to where the application is running"""
    
    def __init__(self,mac = False,debug = False,pc = False):
        if debug: 
            self.save_folder = "C:\\Symbian\\8.0a\\S60_2nd_FP2\\epoc32\\wins\\c\\data\\digitalics"
            self.output_folder = "C:\\Symbian\\8.0a\\S60_2nd_FP2\\epoc32\\wins\\c\\data\\camoutput"
            self.feedback_folder = "C:\\Symbian\\8.0a\\S60_2nd_FP2\\epoc32\\wins\\c\\data\\feedback"
            self.temp_folder = "C:\\Symbian\\8.0a\\S60_2nd_FP2\\epoc32\\wins\\c\\data\\tmp"
        elif mac:
            self.save_folder = "/Users/yaels/Documents/workspace/digitalics/src/extradir/digitalics/data"
            self.output_folder = "/Users/yaels/Documents/workspace/digitalics/src/extradir/digitalics/output"
            self.feedback_folder = "/Users/yaels/Documents/workspace/digitalics/src/extradir/digitalics/feedback"
            self.temp_folder = "/Users/yaels/Documents/workspace/digitalics/src/extradir/digitalics/tmp"
        elif pc: 
            self.save_folder = "C:\Documents and Settings\Yael\workspace\digitalics\src\extradir\digitalics\data"
            self.output_folder = "C:\Documents and Settings\Yael\workspace\digitalics\src\extradir\digitalics\output"
            self.feedback_folder = "C:\Documents and Settings\Yael\workspace\digitalics\src\extradir\digitalics\feedback"
            self.temp_folder = "C:\Documents and Settings\Yael\workspace\digitalics\src\extradir\digitalics\tmp" 
        else:
#            if e32.in_emulator():
#                self.save_folder = "e:\\data\\digitalics"
#                self.output_folder = "c:\\data\\camoutput"
#                self.feedback_folder = "c:\\data\\feedback"
#                self.temp_folder = "c:\\data\\tmp"
#            else:
            self.save_folder = "e:\\digitalics\\data"
            self.output_folder = "e:\\digitalics\\output"
            self.feedback_folder = "e:\\digitalics\\feedback"
            self.temp_folder = "e:\\digitalics\\tmp"
            
        self.database_contents_file = os.path.join(self.temp_folder,"table_contents.txt")                
        self.database_import_file = os.path.join(self.temp_folder,"table_data.txt")
        self.database_communities_file= os.path.join(self.temp_folder,"communities.txt")
        self.organization_pickle_file = os.path.join(self.temp_folder,"org_pickle.pkl")
        self.organization_data_file = os.path.join(self.temp_folder,"organization_info.txt")
        self.db_path = os.path.join(self.temp_folder,"evaluacion.db" )

    def get_feedback_folder(self):
        return self.feedback_folder
    
    
    def get_temp_folder(self):
        return self.temp_folder
    
    def get_save_folder(self):
        return self.save_folder
    
    def set_save_folder(self,save_folder):
        self.save_folder = save_folder
    
    def get_output_folder(self):
        return self.output_folder

    def set_output_folder(self,output_folder):
        self.output_folder = output_folder

    def get_database_import_file(self):
        return self.database_import_file
    
    def get_database_communities_file(self):
        return self.database_communities_file
    
    def get_database_contents_file(self):
        return self.database_contents_file
    
    def get_db_path(self):
        return self.db_path
    
    def get_organization_pickle_file(self):
        return self.organization_pickle_file
    
    def get_organization_data_file(self):
        return self.organization_data_file
      
    
    
    def get_id_list(self,args = None):
        id_list = []
        if args is not None:
            folder = os.path.join(self.save_folder,"\\".join(["%s"% a for a in args]))
        else:
            folder = self.save_folder
        if os.path.exists(folder):
            id_list.extend(os.listdir(folder))
        #id_list.append(l.NEW+'...')
        if 'None' in  id_list:
            id_list.remove('None')
        return [u"%s"%id for id in id_list if not id.startswith('.')]

    def get_form_list(self,community,producer,municipio=None,localidad=None):
        if localidad is None and municipio is None:
            sub_folder = "%s\\%s"%(community,producer)
        else:
            sub_folder = "%s\\%s\\%s\\%s"%(community,municipio,localidad,producer)
        form_list =  os.listdir(os.path.join(self.save_folder,sub_folder))
        return [u"%s"%form for form in form_list if not form.startswith('.') and form.endswith('.form')]
    
    def get_output_file(self,community,producer,form,municipio=None,localidad=None):
        if localidad is None and municipio is None:
            sub_folder = "%s\\%s"%(community,producer)
        else:
            sub_folder = "%s\\%s\\%s\\%s"%(community,municipio,localidad,producer)
        sub_folder ="%s\\%s"%(community,producer)
        path = os.path.join(self.output_folder,sub_folder)
        if not os.path.exists(path):
            os.makedirs(path,mode=0777)
        
        return os.path.join(self.output_folder,"%s\\%s.txt" % (sub_folder,form))

    def save_common_ids(self,community, municipio = None, localidad = None):
        # to have default values     
        folder = self.get_temp_folder()
        file = open(os.path.join(folder,"ids.txt"),"w")
        if municipio is not None and localidad is not None:
            file.write("%s/%s/%s" % (community,municipio,localidad))
        else:
            file.write("%s" % community)
        file.close()
    
    def get_common_ids(self): 
        folder = self.get_temp_folder()
        filename = os.path.join(folder,"ids.txt")
        if os.path.exists(filename):
            file = open(filename,"r")
            line = file.read()
            file.close()
            return tuple(line.split("/"))
        else:
            return (None,None,None)
    
    def delete_data(self,dir_name = None):
        
        if dir_name is None:
            dir_name = self.save_folder
        for name in os.listdir(dir_name):
            path = os.path.join(dir_name, name)
            if os.path.isdir(path):
                self.delete_data(path)
                os.rmdir(path)
            elif os.path.isfile(path):
                os.remove(path)
            
if __name__=="__main__": 
    fe = FExplorer()
    fe.delete_data()           